package ruifight.member.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ruifight.member.common.CException;
import ruifight.member.dao.AccountRecordMapper;
import ruifight.member.dao.IntegralRecordMapper;
import ruifight.member.dao.MemberInfoMapper;
import ruifight.member.dao.ShopAccountMapper;
import ruifight.member.dao.ShopInfoMapper;
import ruifight.member.dao.UserInfoMapper;
import ruifight.member.model.AccountRecord;
import ruifight.member.model.AccountRecordExample;
import ruifight.member.model.IntegralRecord;
import ruifight.member.model.ShopAccount;
import ruifight.member.model.ShopAccountExample;
import ruifight.member.service.AccountRecordService;
import ruifight.member.service.base.BaseServiceImpl;
import ruifight.member.util.CommonUtil;
import ruifight.member.wxPay.MyWXPayConfig;
import ruifight.member.wxPay.WXPay;

@Service
public class AccountRecordServiceImpl extends BaseServiceImpl<AccountRecord, AccountRecordExample, Long> implements AccountRecordService{

    @Resource
    private AccountRecordMapper accountRecordMapper;
    @Resource
    private MemberInfoMapper memberInfoMapper;
    @Resource
    private ShopAccountMapper shopAccountMapper;
    @Resource
    private IntegralRecordMapper integralRecordMapper;
    @Resource
    private ShopInfoMapper shopInfoMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    public PageInfo<Map<String, Object>> getAccountRecordList(int cur_page, int page_size,Map<String, Object> paramMap){
		PageHelper.startPage(cur_page, page_size);
		List<Map<String, Object>> pageList = accountRecordMapper.getAccountRecordList(paramMap);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(pageList);
		return pageInfo;
    }

    @Transactional(rollbackFor=Exception.class)
	@Override
	public String addAccountRecord(AccountRecord accountRecord,String total_fee,String shopType,String out_trade_no,String openId) throws Exception {
		accountRecordMapper.insertSelective(accountRecord);
    	WXPay wxPay=new WXPay(new MyWXPayConfig(),CommonUtil.getPropertiesParam("callbackUrl"),false,false);
		Map<String, String> reqData=new HashMap<String, String>();
		Map<String, String> resData=null;
		reqData.put("body", accountRecord.getPayeeShopName()+"-"+shopType);
		reqData.put("out_trade_no", out_trade_no);
		reqData.put("total_fee", total_fee);
		reqData.put("spbill_create_ip", CommonUtil.getPropertiesParam("spbill_create_ip"));
		reqData.put("trade_type", "JSAPI");
		reqData.put("openid", openId);
		reqData=wxPay.fillRequestData(reqData);
		resData=wxPay.unifiedOrder(reqData);
		String return_code=resData.get("return_code");
		if("SUCCESS".equals(return_code)) {
			String result_code=resData.get("result_code");
			if("SUCCESS".equals(result_code)) {
				String prepay_id=resData.get("prepay_id");
				return prepay_id;
			}
		}
		throw new CException(0, "下单失败");
	}

    @Transactional(rollbackFor=Exception.class)
	@Override
	public String finishAccountRecord(AccountRecord accountRecord,Long memberInfoId){

    	BigDecimal payable = new BigDecimal(accountRecord.getPayable());
    	BigDecimal payment = new BigDecimal(accountRecord.getPayment());
    	BigDecimal giveMoney = payable.subtract(payment);
    	Map<String, Object> paramMap=new HashMap<String, Object>();
    	paramMap.put("shopAccountMoney", payable);
    	paramMap.put("actualMoney", payment);
    	paramMap.put("giveMoney", giveMoney);
    	paramMap.put("shopInfoId", accountRecord.getPayeeShopId());
    	shopAccountMapper.updateShopAccountMoney(paramMap);
    	//添加店铺余额
    	ShopAccountExample shopAccountExample=new ShopAccountExample();
		shopAccountExample.createCriteria()
			.andShopInfoIdEqualTo(accountRecord.getPayeeShopId());
		ShopAccount shopAccount=shopAccountMapper.selectByExample(shopAccountExample).get(0);
    	accountRecord.setAttr3(shopAccount.getShopAccountMoney().subtract(payable)+"");
    	accountRecordMapper.updateByPrimaryKeySelective(accountRecord);
    	
    	Map<String, Object> paramMap2=new HashMap<String, Object>();
    	paramMap2.put("memberIntegral", payment.intValue());
    	paramMap2.put("userInfoId", accountRecord.getDraweeId());
    	memberInfoMapper.updateIntegral(paramMap2);
    	IntegralRecord integralRecord=new IntegralRecord();
    	integralRecord.setMemberInfoId(memberInfoId);
    	integralRecord.setIntegral(payment.intValue());
    	integralRecord.setIntegralType(2);
    	integralRecord.setIntegralComment("消费后获得积分");
    	integralRecord.setCreateDate(new Date());
    	integralRecordMapper.insertSelective(integralRecord);
		return "SUCCESS";
	}

	public List<Map<String, Object>> getTradingVolume(){
    	return accountRecordMapper.getTradingVolume();
	}
	public List<Map<String, Object>> getAturnover(){
    	return accountRecordMapper.getAturnover();
	}

	public Float getCumulativeDiscount(Map<String, Object> paramMap){
    	return accountRecordMapper.getCumulativeDiscount(paramMap);
	}

	@Override
	public Float getCumulativeMoney(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return accountRecordMapper.getCumulativeMoney(paramMap);
	}
}
